// pages/map/map.js
import {
  getAllAddressList,
  getMapIcon,
  getDetailMapIcon,
  getAddressInfo,
  getSecondAddressList
} from "../../apis/address_api"
import {
  logincode
} from "../../apis/login_api"
import {
  getResultsByAdviseId
} from "../../apis/results_api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 默认高度
    heightShow: 100,
    // 一级权限代码
    firstCode: getApp().globalData.firstCode,
    // 二级权限代码
    secondCode: getApp().globalData.secondCode,
    // 经度
    longitude: 113.732754,
    // 纬度
    latitude: 27.640671,
    // 辖区列表
    addressList: [],
    // 查询辖区列表结果
    searchAddressList: [],
    // 图标列表信息
    markers: [],
    // 详细事件图标列表信息
    markers2: [],
    // 一级辖区
    firstAddress: "",
    // 二级辖区
    secondAddress: "",
    // 标题
    address: "",
    // 已接收
    total: 0,
    // 已处理
    finish: 0,
    // 未处理
    handle: 0,
    // 显示弹窗
    isShowPop: false,
    // 显示具体事件弹窗
    isdetailsShowPop: false,
    // 显示具体事件详情
    Specificevents: [],
    // 显示地图列表
    isShowLocationList: false,
    // 显示地图类型
    meptype:false,
    // 管理员姓名
    name: "管理员",
    // 管理员手机号
    phone: "13697003363",
  },

  //自动跳转电话
  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone
    })
  },

  // 修改地图高度
  changeHeight() {
    let heightShow = this.data.heightShow
    if (heightShow == 70) {
      this.setData({
        heightShow: 20
      })
    } else {
      this.setData({
        heightShow: 70
      })
    }
  },

  // 跳转页面查看详情
  getInfo() {
    let secondAddress = this.data.secondAddress
    wx.navigateTo({
      url: '/pages/myRelease/myRelease?address=' + secondAddress,
    })
  },

  // 获取点位信息
  getLocationInfo(e) {
    let id = e.markerId
    getAddressInfo(id).then(res => {
      console.log(res)
      this.setData({
        firstAddress: res.data.firstAddress,
        secondAddress: res.data.secondAddress,
        total: res.data.total,
        handle: res.data.untreatedNum,
        finish: res.data.accomplishNum,
        isShowPop: true,
        isShowLocationList: false,
        name: res.data.name,
        phone: res.data.phone,
      })
    })
  },

  // 关闭弹窗
  closePop() {
    this.setData({
      isShowPop: false,
      isdetailsShowPop: false,
      heightShow: 70,
    })
  },
  detailsclosePop(){
    this.setData({
      isdetailsShowPop: false,
    })
  },
  // 跳转地图
  getLocationItem(e) {
    let longitude = e.currentTarget.dataset.longitude
    let latitude = e.currentTarget.dataset.latitude
    this.setData({
      longitude,
      latitude
    })
  },

  //获取用户定位
  getLocation() {
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        that.setData({
          longitude: res.longitude + 0.006,
          latitude: res.latitude - 0.004,
        })
      },
      fail(err) {
        wx.showToast({
          title: '定位失败',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },

  //请求获取地图点位
  getMapIcon(addressList) {
    let greenIcon = "/images/green_marker.png"
    let yellowIcon = "/images/yellow_marker.png"
    let redIcon = "/images/red_marker.png"
    getMapIcon(greenIcon, yellowIcon, redIcon, addressList).then(res => {
      
      this.setData({
        markers: res.data,
      })
      console.log(this.data.markers)
    })
  },
    //请求获取地图点位
    gettoDetailMapIcon() {
      let greenIcon = "/images/green_marker.png"
      getDetailMapIcon(greenIcon).then(res => {
        console.log(res.data)
        this.setData({
          markers2: res.data,
        })
        console.log(this.data.markers2)
      })
    },
  // 切换具体事件地图
  changtap(){
    this.setData({
      meptype:!this.data.meptype
    })
    console.log(this.data.meptype)
  },
  // 一级管理员获取所有辖区
  getAllAddressList() {
    getAllAddressList().then(res => {
      this.setData({
        addressList: res.data,
        searchAddressList: res.data
      })
    })
  },
  // 通过id获取具体信息
  getdetailsInfo(e){
    let id = e.markerId
    wx.navigateTo({
      url: '/pages/handle/handle?id=' + id,
    })

  },
  // 获取二级管理员辖区列表
  getSecondAddressList(addressList) {
    getSecondAddressList(addressList).then(res => {
      this.setData({
        addressList: res.data,
      })
    })
  },

  // 初始化页面数据
  getPageData(options) {
    let code = wx.getStorageSync("code")
    console.log(code);
    if (code == getApp().globalData.firstCode) {
      this.getAllAddressList()
      this.getMapIcon()
      this.gettoDetailMapIcon()
      this.setData({
        heightShow: 70,
        code
      })
    } else if (code == getApp().globalData.secondCode) {
      let addressList = wx.getStorageSync("addressList")
      this.getMapIcon(addressList)
      this.getSecondAddressList(addressList)
      this.setData({
        heightShow: 70,
        code
      })
    } else {
      this.setData({
        heightShow: 100,
        code
      })
    }
    this.getLocation()

  },
  wxlogin(){
    let phone = wx.getStorageSync('phoneNumber')
    wx.login({
      success: function(res) {
        console.log(res)
        if (res.code) {
          logincode(res.code,phone).then(res =>{
            console.log(res)
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.wxlogin(); // 微信登录
    wx.setNavigationBarTitle({
      title: '地图',
    })
    this.getPageData(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})